c++ - C++ 中复杂的 Typedef
全部标签 我正在尝试编写一个简单的模板化事件调度程序,但我遇到了我不理解的编译器错误,搜索它也没有任何帮助。我正在使用VisualStudio2013Express。这是我的代码:templateclassEventDispatcher{public:typedefvoid(EventHandler)(Tevent);EventDispatcher(){}~EventDispatcher(){}voidaddListener(conststd::stringeventName,EventHandlerhandler){}voidfireEvent(Tevent){}private:typedef
我在使用带有const关键字(用于函数参数类型)的模板时遇到问题,为了说明这一点,我创建了一个小代码:templatestructMethodCallerFactory{typedefReturnType(*Type)(ClassType*,Args...);templatestructMethod{staticTypecreateMethodCaller(){ReturnType(*caller)(ClassType*,Args...)=[](ClassType*obj,Args...args)->ReturnType{ReturnType(ClassType::*ptr)(Args
我正在看这个cpp闪电谈话video它在0:37显示这段代码template>classQScopedPointer{typedefT*QScopedPointer::*RestrictedBool;//howdoesthiswork?//whynotQScopedPointersinceQScopedPointerisatemplate?public:inlineoperatorRestrictedBool()const{returnisNull()?Q_NULLPTR:&QScopedPointer::d;}inlineboolisNull()const{return!d;}pro
当用作模板类型参数时,函数的typedef和使用裸函数类型之间肯定有区别。即,考虑#includetypedefstd::functionTF1;typedefvoid(*FooFn)(int);typedefstd::functionTF2;intmain(){TF1tf1;TF2tf2;return0;}我可以创建TF1但不能创建TF2(错误:aggregate'TF2tf2'的类型不完整,无法定义)。(参见ideoneexample。)有没有办法使用函数(签名)的typedef作为模板类型参数;具体来说,作为std::function?的类型参数(没有C++11标记,因为我对bo
我正在处理一个对话框,在启用“确定”按钮之前必须满足其中的几个规则。目前,页面上的任何操作(例如输入数据或从下拉列表中选择项目(以及其他操作))都会调用一个名为ProcessEvent()的函数-此函数处理所有逻辑并启用或禁用“确定”按钮。我的问题是我发现很难使规则简洁易懂。一些规则可以被对话框中的另一个Action否定,我现在已经结束了到处都是ifelse语句或者难以阅读、遵循和扩展。下面的代码是问题的简化,但很好地演示了它。我如何更好地处理这个问题(如果可能的话)boolCWorkstation::ProcessEvent(void){UpdateData();CharCount=
#include#includeusingnamespacestd;voidprintstr(conststring&s){coutclassTest{public:typedefvoid(*Func)(constA&);};typedefvoid(*Func)(conststring&);templatevoidbind(Test::Funcf,//在上面的代码中,我尝试使用另一个类中的函数指针typedef。如图所示,它没有编译,但是其他两行中的任何一行都没有注释,而不是Test::Funcf,。行,它编译得很好!这是我不能用C++做的事情吗?需要什么语法?使用g++4.4.3,我
我创建了一个Chromosome类,它最终只是一个带有ostream运算符的vector包装器,所以我决定改用typedefvector。但是,我在使用模板化的ostream运算符时遇到了问题……这是最好的方法吗?(我见过一些方法,但都没有奏效)templateclassChromosome{public:typedeftypenamestd::vectortype;typedeftypenamestd::pairptr_pair;};template//line19below:std::ostream&operator::type&chromosome){for(autoiter=c
有这样的代码:templateclassSomeClass{typedefboost::shared_ptrsPtr;typedefstd::vectorc;typedefc::iteratorcIt;//hereistheproblem};错误是:main.cpp:23:error:type‘std::vector,std::allocator>>’isnotderivedfromtype‘SomeClass’main.cpp:23:error:expected‘;’before‘cIt’如何在类中使用typedef来模板化参数?编辑:我想通了,对于g++,那一定是:typedeft
根据($3.4.4)后跟类键的typedef名称是无效的。但我不确定是哪个范围?例如:在下文中,如果在函数内部等block中使用了详细说明符,编译器不会报错。typedefclass{/*...*/}S;//invalidclassS;//okvoidfoo(){classS;}使用typedef-name在本地范围内声明类是否有效,为什么? 最佳答案 7.1.3第3段讲述:Inagivenscope,atypedefspecifiershallnotbeusedtoredefinethenameofanytypedeclaredi
这可能是个有点傻的问题,但我不得不问。我正在尝试在C++中使用unordered_map类,但不是每次都将其作为tr1::unordered_map引用,我只想使用关键字hashMap。我知道typedeftr1::unordered_maphashMap有效,但这种修复了键的数据类型和对应于hashMap的值,而我希望有更多类似以下内容:#definehashMaptr1::unordered_map我可以根据需要定义键和值的数据类型,但这不起作用。以前有人遇到过这个问题吗?谢谢 最佳答案 这是C++11之前的C++所缺少的东西。